//
//  GWViewController.m
//  瀑布流
//
//  Created by qianfeng on 16/2/29.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWViewController.h"
#import "GWWaterFllowView.h"
#import "GWPhotosCell.h"
#import "MJExtension.h"
#import "GWPhoto.h"
#import "MJRefresh.h"
@interface GWViewController ()<GWWaterFllowViewDataSource,GWWaterFllowViewDelegate>
@property (nonatomic,copy)NSMutableArray *photos;

@property (nonatomic,weak)GWWaterFllowView *waterView;
@end

@implementation GWViewController
-(NSMutableArray *)photos
{
    if (_photos ==nil) {
        _photos =[GWPhoto mj_objectArrayWithFilename:@"2.plist"];
    }
    return _photos;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    GWWaterFllowView *fllowView= [[GWWaterFllowView alloc]init];
//    fllowView.backgroundColor =[UIColor redColor];
    fllowView.autoresizingMask =UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    fllowView.frame =self.view.bounds;
    fllowView.dataSource =self;
    fllowView.delegate =self;
    [self.view addSubview:fllowView];
    self.waterView =fllowView;
    
    fllowView.mj_header =[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewPhoto)];
    
    fllowView.mj_footer =[MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMorePhoto)];
}
-(void)loadNewPhoto
{
   static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray *array =[GWPhoto mj_objectArrayWithFilename:@"1.plist"];
        [self.photos insertObjects:array atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, array.count)]];
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.waterView reloadData];
        [self.waterView.mj_header endRefreshing];
    });
}
-(void)loadMorePhoto
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray *array =[GWPhoto mj_objectArrayWithFilename:@"3.plist"];
        [self.photos addObjectsFromArray:array];
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.waterView reloadData];
        [self.waterView.mj_footer endRefreshing];
    });
}


-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.waterView reloadData];
}

-(NSInteger)numberOfclumnsInWaterFllowView:(GWWaterFllowView *)waterFllowView
{
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
        return 3;
    }
    else
    {
        return 5;
    }
}

-(NSInteger)numberOfCellsInWaterFllowView:(GWWaterFllowView *)waterFllowView
{
    return self.photos.count;
}



-(GWWaterFllowViewCell *)waterFllowView:(GWWaterFllowView *)waterFllowView cellForRowAtIndex:(NSInteger)Index
{
    
    GWPhotosCell *cell =[GWPhotosCell cellWithWaterFloowView:waterFllowView];
    
    GWPhoto *photo =self.photos[Index];
    cell.photo =photo;
    return cell;
}

#pragma mark ----GWWaterFllowViewDelegate
-(void)waterFllowView:(GWWaterFllowView *)waterFllowView didSelectCellAtIndex:(NSInteger)index
{
    NSLog(@"%ld被点击了-----",index);
}
-(CGFloat)waterFllowView:(GWWaterFllowView *)waterFllowView heightForCellAtIndex:(NSInteger)index
{
    GWPhoto *photo =self.photos[index];
    
    return waterFllowView.cellWidth * [photo.h floatValue]/[photo.w floatValue];
    
}
-(CGFloat)waterFllowView:(GWWaterFllowView *)waterFllowView marginWithType:(GWWaterFllowViewMarginType)marginType
{
    switch (marginType) {
        case GWWaterFllowViewMarginTypeBottom:
            case GWWaterFllowViewMarginTypeLeft:
            case GWWaterFllowViewMarginTypeRight:
            case GWWaterFllowViewMarginTypeTop:
            return 10;
            break;
            
        default:
            return 5;
            break;
    }
}
@end
